#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>
#include <QStackedWidget>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

#include <QHostAddress>

#pragma pack(push, 1) // 防止结构体自动填充对齐
struct sendRig {
    int type;
    char Account[64];
    char password[24];
    bool Idtype;
    char tele[24];
    int IC;
    char telewo[24];
    char state[24];
    char ID[24];
    int num[10];
};
#pragma pack(pop)

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow() = default;

private slots:
    void showDepositPage();
    void showRetrievePage();
    void showMainPage();
    void updateCountdown();
    void nextAdImage();
    void numberClicked();
    void deleteClicked();
    void confirmClicked();
    void readServerMessage();
    void setupUI();
    void initTcpConnection();
    void initDatabase();

    bool initSerialPort(const char* device);
    void sendCommand(const char* cmd);
    void showCenteredMessageBox(const QString &title, const QString &text);
    QList<int> getUnusedLockerIndicesFromDB();
    int assignRandomLocker(const QList<int> &freeIndices);

private:

    QTcpSocket *tcpSocket;
    QStackedWidget *stackedWidget;
    QTimer *inactivityTimer;
    QTimer *adTimer;

    // 页面指针
    QWidget *mainPage;
    QWidget *depositPage;
    QWidget *retrievePage;

    // 倒计时
    int countdown;
    QPushButton *depositBackBtn;
    QPushButton *retrieveBackBtn;

    // 广告图像
    QLabel *adLabel;
    QStringList adImages;
    int adIndex = 0;

    // 输入区
    QLineEdit *codeEdit;

    //串口
    QTimer *serialReadTimer;
    int serial_fd = -1;
};

#endif // MAINWINDOW_H
